/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.psi;

import com.googlecode.sca4idea.util.ComponentResolver;
import com.googlecode.sca4idea.util.SCDLResolver;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * PSI Element used as placeholder when the exact details of an F3 component are unknown.
 *
 * @author paul.jones
 */
public class SCAWebappComponentTagPsiElement extends SCAComponentTagPsiElement {
    private final XmlFile componentType;

    public SCAWebappComponentTagPsiElement(XmlTag underlying) {
        super(underlying);

        componentType = SCDLResolver.loadScdlLocation(underlying, "web.componentType");
    }

    public PsiElement resolveServiceInterface(String serviceInterfaceName) {
        return null;
    }

    public Object[] getVariantsOfServiceInterfaces(boolean qualified) {
        return new Object[0];
    }

    public PromotedWireable[] getVariantsOfProperties(boolean qualified) {
        return new PromotedWireable[0];
    }

    public PsiElement resolveReference(String referenceName) {
        if (componentType == null) {
            return null;
        }

        Map<String, XmlTag> references = ComponentResolver.getPromotedReferences(componentType, false);
        return references.get(referenceName);
    }

    public PromotedWireable[] getVariantsOfReferences(boolean qualified) {
        if (componentType == null) {
            return null;
        }

        // Retrieve all the references for the referenced file
        Map<String, XmlTag> references = ComponentResolver.getPromotedReferences(componentType, false);
        String componentName = getComponentName();
        List<PromotedWireable> result = new ArrayList<PromotedWireable>();
        for(String reference : references.keySet()) {
            if (qualified) {
                result.add(new PromotedWireableImpl(componentName + "/" + reference, false));
            } else {
                result.add(new PromotedWireableImpl(reference, false));
            }
        }

        return result.toArray(new PromotedWireable[result.size()]);
    }

    public PsiElement resolveProperty(String propertyName) {
        return null;
    }
}